package com.example.controller;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.util.StrUtil;
import com.example.entity.dto.UpdateMerchantInfoSimpleDTO;
import com.example.service.MerchantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/merchant")
@Tag(name = "商家类", description = "用户的CURD，登录，退出")
public class MerchantController {

	@Autowired
	private MerchantService merchantService;

	@Operation(summary = "商家登录")
	@Parameters({
			@Parameter(name = "loginCode", description = "微信返回的临时code", in = ParameterIn.QUERY, required = true),
			@Parameter(name = "token", description = "token", in = ParameterIn.HEADER)
	})
	@GetMapping("login")
	public SaResult login(@RequestParam("loginCode") String loginCode, @RequestHeader(value = "token", required =
			false) String token) {
		Map<String, String> message = merchantService.login(loginCode, token);
		return SaResult.ok("登录成功").setData(message);
	}

	@Operation(summary = "更新商户简单的信息")
	@PostMapping("/updateMerchantSimpleInfo")
	public SaResult updateMerchantSimpleInfo(@RequestBody UpdateMerchantInfoSimpleDTO updateMerchantInfoSimpleDTO,
											 @RequestHeader("token") String token) {
		merchantService.updateMerchantSimpleInfo(updateMerchantInfoSimpleDTO,token);
		return SaResult.ok();
	}

	@Operation(summary = "生成对应用户的评价二维码")
	@Parameters({
			@Parameter(name = "merchantId", description = "商家id", in = ParameterIn.QUERY)
	})
	@GetMapping("generateWechatQRCode")
	public SaResult generateWechatQRCode(@RequestParam("merchantId") String merchantId) {
		return SaResult.data(merchantService.generateWechatQRCode(merchantId));
	}


	@Operation(summary = "获取商家基本信息")
	@GetMapping("getMerchantInfo/{queryTime}")
	public SaResult getMerchantInfo(@RequestHeader("token") String token, @PathVariable String queryTime) {
		if (StrUtil.isBlank(queryTime.trim())) {
			queryTime = "all";
		}
		return SaResult.data(merchantService.getMerchantInfo(token, queryTime));
	}


}